package com.zhima.controller.feign;

import com.zhima.dto.FeignDownloadResultDTO;
import com.zhima.dto.FeignUploadFileDTO;
import com.zhima.dto.FeignUploadResultDTO;
import com.zhima.service.OjFileService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/file/feign")
public class OjFileFeignController {
    @Autowired
    private OjFileService ojFileService;

    /**
     * 上传文件至指定桶中
     *
     * @param feignUploadFileDTO
     * @param bucket
     * @return
     */
    @PostMapping("/{bucket}/{uploaderId}")
    public FeignUploadResultDTO upload(@PathVariable("uploaderId") Long uploaderId, @RequestBody FeignUploadFileDTO feignUploadFileDTO, @PathVariable("bucket") String bucket) {
        return ojFileService.uploadFile(uploaderId, bucket, feignUploadFileDTO.getFileName(), feignUploadFileDTO.getBytes());
    }

    /**
     * 下载指定文件id的文件
     *
     * @param fileId
     * @return
     */
    @GetMapping("/f/{fileId}")
    public FeignDownloadResultDTO downloadByFileId(@PathVariable("fileId") Long fileId) {
        return ojFileService.downloadByFileId(fileId);
    }

    /**
     * 下载指定objectId的文件
     *
     * @param objectId
     * @return
     */
    @GetMapping("/o/{objectId}")
    public FeignDownloadResultDTO downloadByObjectId(@PathVariable("objectId") String objectId) {
        return ojFileService.downloadByObjectId(objectId);
    }

    @DeleteMapping("/{fileId}")
    public void deleteFileByFileId(@PathVariable("fileId") Long fileId) {
        ojFileService.deleteFileByFileId(fileId);
    }
}
